package com.example.home.inspectCar;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class InspectCarMainActivity extends AppCompatActivity {
    private ImageView back;
    private EditText description;
    private EditText inputField;
    private EditText engineNo;
    private Button btnBind;
    private Button btnInquire;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspect_car_main);
        initView();
        bindCar();
    }

    private void bindCar() {
        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strDescription = description.getText().toString();
                String questionType = inputField.getText().toString();
                String strengineNo = engineNo.getText().toString();
                JSONObject jsonObject = new JSONObject();
                if (!TextUtils.isEmpty(strDescription)){
                    try {
                        jsonObject.put("engineNo",strengineNo);
                        jsonObject.put("plateNo",questionType);
                        jsonObject.put("type",strDescription);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                OKHttpTool.doNetRequest("prod-api/api/traffic/car",
                        OKHttpTool.POST,
                        GeneralClass.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                GeneralClass generalClass = (GeneralClass) obj;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (generalClass.getCode() == 200){
                                            Toast.makeText(InspectCarMainActivity.this,"绑定成功", Toast.LENGTH_SHORT).show();
                                        }else if (generalClass.getCode() == 500){
                                            Toast.makeText(InspectCarMainActivity.this,"绑定失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        },jsonObject.toString());
            }
        });
    }

    private void initView() {
        back = findViewById(R.id.back);
        description = findViewById(R.id.description);
        inputField = findViewById(R.id.inputField);
        engineNo = findViewById(R.id.engineNo);
        btnBind = findViewById(R.id.btn_bind);
        btnInquire = findViewById(R.id.btn_inquire);

        btnInquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(InspectCarMainActivity.this,QueryViolation.class);
                startActivity(intent);
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InspectCarMainActivity.this.finish();
            }
        });
    }
}